package com.sy.service.impl;

import com.alibaba.excel.EasyExcel;
import com.sy.bean.Goods;
import com.sy.bean.management.GoodsQuery;
import com.sy.bean.pucharse.excel.GoodsExcel;
import com.sy.bean.pucharse.excel.PurchaseExcel;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.GoodsMapper;
import com.sy.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends BaseServiceImpl<Goods, GoodsMapper> implements GoodsService {
    @Autowired
    GoodsMapper mapper;
    @Autowired
    HttpServletRequest request;
    @Override
    public int deleteGood(int id) {
        int i = mapper.deleteGood(id);
        return i;
    }

    @Override
    public String exportGoods(Map map) {
        List<GoodsExcel> list=mapper.exportGoods(map);
        String root =request.getServletContext().getRealPath("/")+"/excel";
        File file=new File(root);
        if (!file.exists()) file.mkdirs();
        String newFileName=file.getAbsolutePath()+"/商品列表.xlsx";

        EasyExcel.write(newFileName, GoodsExcel.class)
                .sheet("商品列表")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/商品列表.xlsx";
    }
}
